from fastapi import APIRouter, Request
from utils import create_json_response

router = APIRouter()

@router.post("/message/action")
async def message_action(request: Request):
    """消息操作接口 - 支持审批和驳回操作"""
    
    # 支持原始数据流格式
    try:
        if request.method == "POST":
            body = await request.body()
            if body:
                import json
                data = json.loads(body.decode('utf-8'))
            else:
                data = {}
        else:
            data = dict(request.query_params)
    except:
        data = {}
    
    # 获取参数
    message_id = int(data.get('id', 0))
    command = str(data.get('command', ''))
    
    # 验证参数
    if message_id <= 0:
        return create_json_response({
            "error": "消息ID无效",
            "result": ""
        })
    
    if command not in ['resolve', 'reject']:
        return create_json_response({
            "error": "操作命令无效，支持 resolve 或 reject",
            "result": ""
        })
    
    # 根据命令返回相应结果
    if command == 'resolve':
        result = f"审批结果：同意。(By 张总) - 消息ID: {message_id}"
    else:  # reject
        result = f"审批结果：驳回。(By 李经理) - 消息ID: {message_id}"
    
    return create_json_response({
        "error": "",
        "result": result
    })